// pages/shopManager/shopService/serviceAdd/serviceAdd.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        objectMultiArray: [],
        multiArray: [],
        multiIndex: [0, 0, 0]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getTopClassify()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    addService: function() {
        wx.redirectTo({
            url: '/pages/shopManager/shopService/serviceEdit/serviceEdit'
        })
    },

    selectDemo: function() {
        wx.navigateTo({
            url: '/'
        })
    },

    getTopClassify: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let objectMultiArray = this.data.objectMultiArray
        let multiArray = this.data.multiArray
        let array = []
        let data = {
            show_type: 3,
            type: 1,
            page: 0,
            limit: 999
        }
        common.post('Goodsclass/store_class_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                multiArray[0] = []
                multiArray[1] = []
                multiArray[2] = []
                objectMultiArray[0] = []
                objectMultiArray[1] = []
                objectMultiArray[2] = []
                that.setData({
                    multiArray: multiArray,
                    objectMultiArray: objectMultiArray
                })
                return
            }
            res.forEach((e) => {
                array.push(e.class_name)
            })
            multiArray[0] = array
            objectMultiArray[0] = res
            that.setData({
                multiArray: multiArray,
                objectMultiArray: objectMultiArray
            })
            that.getSecondClassify(res[0].class_id)
        }).catch(function(e) {})
    },

    getSecondClassify: function(pid) {
        const that = this
        let objectMultiArray = this.data.objectMultiArray
        let multiArray = this.data.multiArray
        let array = []
        let data = {
            show_type: 3,
            type: 2,
            pid: pid,
            page: 0,
            limit: 999
        }
        common.post('Goodsclass/store_class_list', data).then(function(res) {
            if (res.code == 204) {
                multiArray[1] = []
                multiArray[2] = []
                objectMultiArray[1] = []
                objectMultiArray[2] = []
                that.setData({
                    multiArray: multiArray,
                    objectMultiArray: objectMultiArray
                })
                return
            }
            res.forEach((e) => {
                array.push(e.class_name)
            })
            multiArray[1] = array
            objectMultiArray[1] = res
            that.setData({
                multiArray: multiArray,
                objectMultiArray: objectMultiArray
            })
            that.getThirdClassify(res[0].class_id)
        }).catch(function(e) {})
    },

    getThirdClassify: function(pid) {
        const that = this
        let objectMultiArray = this.data.objectMultiArray
        let multiArray = this.data.multiArray
        let array = []
        let data = {
            class_id: pid,
            is_goods: 9,
            page: 0,
            limit: 999
        }
        common.post('Goods/goods_list', data).then(function(res) {
            if (res.code == 204) {
                multiArray[2] = []
                objectMultiArray[2] = []
                that.setData({
                    multiArray: multiArray,
                    objectMultiArray: objectMultiArray
                })
                return
            }
            res.forEach((e) => {
                array.push(e.goods_name)
            })
            multiArray[2] = array
            objectMultiArray[2] = res
            that.setData({
                multiArray: multiArray,
                objectMultiArray: objectMultiArray
            })
        }).catch(function(e) {})
    },

    bindMultiPickerChange: function(e) {
        this.setData({
            multiIndex: e.detail.value
        })
        if (this.data.objectMultiArray[2][this.data.multiIndex[2]]) {
            wx.redirectTo({
                url: '/pages/shopManager/shopService/serviceEdit/serviceEdit?demoId=' + this.data.objectMultiArray[2][this.data.multiIndex[2]].goods_id
            })
        }
    },
    bindMultiPickerColumnChange: function(e) {
        let objectMultiArray = this.data.objectMultiArray
        let multiIndex = this.data.multiIndex
        if (e.detail.column == 0) {
            this.getSecondClassify(objectMultiArray[0][e.detail.value].class_id)
        } else if (e.detail.column == 1) {
            this.getThirdClassify(objectMultiArray[1][e.detail.value].class_id)
        }
    }
})